home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / p-interp.lha / p-interp-0.5 / Memory.c < prev    next >
C/C++ Source or Header  |  2004-02-01  |  6KB  |  299 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.   $Log: Memory.c,v $
  21.   Revision 1.5  2001/06/06 23:26:44  mario
  22.   Amiga-Patch von "Stefan A. Haubenthal" <polluks@web.de>
  23.  
  24.   Revision 1.4  2001/05/26 15:13:29  mario
  25.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  26.   dahinter, ...
  27.  
  28.   Revision 1.3  2001/05/21 20:47:06  mario
  29.   Die einzelnen Bits der Speicherflags können jetzt einzeln abbestellt
  30.   werden.
  31.  
  32.   Revision 1.2  2001/05/20 13:12:02  mario
  33.   CVS-Idents und Logs eingefügt
  34.  
  35.  
  36. */
  37.  
  38. #ident "$Id: Memory.c,v 1.5 2001/06/06 23:26:44 mario Exp $";
  39.  
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43.  
  44. #include "psystem.h"
  45. #include "pcode.h"
  46. #include "Memory.h"
  47.  
  48. #ifdef XXX
  49. #define MEM_VALID    1
  50. #define MEM_RD_ONLY    2
  51. #define MEM_WARN    4
  52. #endif
  53.  
  54. int SwapBytes=0;
  55.  
  56. typedef struct Memory_St
  57. {
  58. #ifdef WORD_MEMORY
  59.   word        Value;
  60. #else
  61.   byte        Value;
  62. #endif
  63.   byte        Flags;
  64. } Memory_t;
  65.  
  66. #ifdef __SASC
  67. __far
  68. #endif
  69. Memory_t    Mem[0x10000];
  70.  
  71. word swap16(word a)
  72. {
  73. return (word)(SwapBytes ? ((a & 0x00FF) <<  8) | ((a >>  8) & 0x00FF) : a);
  74. }
  75.  
  76. #ifdef WORD_MEMORY
  77. word MemRd(word Addr)
  78. {
  79.   PointerCheck(Addr);
  80. #ifdef MEM_VALID
  81.   if (!(Mem[Addr].Flags&MEM_VALID))
  82.     warning("MemRdByte: Reading uninitialized Memory 0x%04x",Addr);
  83. #endif
  84. #ifdef MEM_WARN
  85.   if (Mem[Addr].Flags&MEM_WARN)
  86.     warning("MemRdByte: Access to 0x%04x", Addr);
  87. #endif
  88.   return swap16(Mem[Addr].Value);
  89. }
  90.  
  91. void MemWr(word Addr, word Value)
  92. {
  93.   PointerCheck(Addr);
  94. #ifdef MEM_RD_ONLY
  95.   if (Mem[Addr].Flags&MEM_RD_ONLY)
  96.     {
  97.       warning("MemWrByte: 0x%04x is read only", Addr);
  98.       XeqError(XBADMEM);
  99.     }
  100. #endif
  101. #ifdef MEM_WARN
  102.   if (Mem[Addr].Flags&MEM_WARN)
  103.     warning("MemWrByte: Access to 0x%04x", Addr);
  104. #endif
  105.   Mem[Addr].Value=swap16(Value);
  106. #ifdef MEM_VALID
  107.   Mem[Addr].Flags|=MEM_VALID;
  108. #endif
  109. }
  110.  
  111. byte MemRdByte(word Addr, Integer Offset)
  112. {
  113.   PointerCheck(Addr);
  114.   Addr   += (Offset&~1)/2;
  115.   Offset &= 1;
  116.   if (Offset)
  117.     return(byte)(swap16(MemRd(Addr))>>8);
  118.   else
  119.     return(byte)(swap16(MemRd(Addr))&0xff);
  120. }
  121.  
  122. void MemWrByte(word Addr, Integer Offset, byte Value)
  123. {
  124.   word    w;
  125.   PointerCheck(Addr);
  126.   Addr   += (Offset&~1)/2;
  127.   Offset &= 1;
  128.   w=swap16(MemRd(Addr));
  129.   if (Offset)
  130.     w=(w&0x00ff)|(Value<<8);
  131.   else
  132.     w=(w&0xff00)|(Value&0xff);
  133.   MemWr(Addr, swap16(w));
  134. }
  135.  
  136. #else
  137.  
  138. byte MemRdByte(word Addr, Integer Offset)
  139. {
  140.   PointerCheck(Addr);
  141.   Addr += Offset;
  142. #ifdef MEM_VALID
  143.   if (!(Mem[Addr].Flags&MEM_VALID))
  144.     warning("MemRdByte: Reading uninitialized Memory 0x%04x",Addr);
  145. #endif
  146. #ifdef MEM_WARN
  147.   if (Mem[Addr].Flags&MEM_WARN)
  148.     warning("MemRdByte: Access to 0x%04x", Addr);
  149. #endif
  150.   return(Mem[Addr].Value);
  151. }
  152.  
  153. void MemWrByte(word Addr, Integer Offset, byte Value)
  154. {
  155.   PointerCheck(Addr);
  156.   Addr += Offset;
  157. #ifdef MEM_RD_ONLY
  158.   if (Mem[Addr].Flags&MEM_RD_ONLY)
  159.     {
  160.       warning("MemWrByte: 0x%04x is read only", Addr);
  161.       XeqError(XBADMEM);
  162.     }
  163. #endif
  164. #ifdef MEM_WARN
  165.   if (Mem[Addr].Flags&MEM_WARN)
  166.     warning("MemWrByte: Access to 0x%04x", Addr);
  167. #endif
  168.   Mem[Addr].Value=Value;
  169. #ifdef MEM_VALID
  170.   Mem[Addr].Flags|=MEM_VALID;
  171. #endif
  172. }
  173.  
  174. word MemRd(word Addr)
  175. {
  176.   PointerCheck(Addr);
  177.   return ( MemRdByte(Addr, 0+SwapBytes) +
  178.        (MemRdByte(Addr, 1-SwapBytes)<<8) );    
  179. }
  180.  
  181. void MemWr(word Addr, word Value)
  182. {
  183.   PointerCheck(Addr);
  184.   MemWrByte(Addr, 0+SwapBytes, Value & 0xff);
  185.   MemWrByte(Addr, 1+SwapBytes, Value >> 8);
  186. }
  187. #endif
  188.  
  189. void MemReadOnly(word from, word to, int RO)
  190. {
  191. #ifdef MEM_RD_ONLY
  192.   while (from<=to)
  193.     {
  194.       if (RO)
  195.     Mem[from].Flags|=MEM_RD_ONLY;
  196.       else
  197.     Mem[from].Flags&=~MEM_RD_ONLY;
  198.       from++;
  199.     }
  200. #endif
  201. }
  202.  
  203. void MemDump(FILE *f, word Start, word End)
  204. {
  205.   int    w;
  206.   int    i;
  207.   int    Count=0;
  208. #ifdef WORD_MEMORY
  209.   word    Value;
  210.   char    b[6];
  211.   byte    ch1;
  212. #else
  213.   byte    Value;
  214.   char    b[4];
  215. #endif
  216.   byte    ch;
  217.   char    Buffer[80];
  218.   char    OldBuffer[80];
  219.   OldBuffer[0]='\0';
  220.   for (w=Start; w<=End;)
  221.     {
  222. #ifdef WORD_MEMORY
  223.       w=w&0xfff8;
  224. #else
  225.       w=w&0xfff0;
  226. #endif
  227.       sprintf(Buffer,"%04x: %48s %16s\n",w,"","");
  228. #ifdef WORD_MEMORY
  229.       for (i=0;i< 8;i++)
  230. #else
  231.       for (i=0;i<16;i++)
  232. #endif
  233.     {
  234. #ifdef MEM_VALID
  235.       if (Mem[w].Flags&MEM_VALID)
  236.         {
  237. #endif /* MEM_VALID */
  238.           Value=Mem[w].Value;
  239. #ifdef WORD_MEMORY
  240.           sprintf(b,"%04x",Value);
  241. #else
  242.           sprintf(b,"%02x",Value);
  243. #endif
  244.           ch=Value&0xff;
  245.           if (!isprint(ch))
  246.         ch='.';
  247. #ifdef WORD_MEMORY
  248.           ch1=Value>>8;
  249.           if (!isprint(ch1))
  250.         ch1='.';
  251. #endif
  252. #ifdef MEM_VALID
  253.         }
  254.       else
  255.         {
  256. #ifdef WORD_MEMORY
  257.           strcpy(b,"....");
  258.           ch1=' ';
  259. #else
  260.           strcpy(b,"..");
  261. #endif
  262.           ch=' ';
  263.         }
  264. #endif /* MEM_VALID */
  265. #ifdef WORD_MEMORY
  266.       Buffer[6+5*i   ]=b[0];
  267.       Buffer[6+5*i+1 ]=b[1];
  268.       Buffer[6+5*i+2 ]=b[2];
  269.       Buffer[6+5*i+3 ]=b[3];
  270.       Buffer[6+5*8+2*i  ]=ch;
  271.       Buffer[6+5*8+2*i+1]=ch1;
  272. #else
  273.       Buffer[6+3*i   ]=b[0];
  274.       Buffer[6+3*i+1 ]=b[1];
  275.       Buffer[6+3*16+i]=ch;
  276. #endif
  277.       w++;
  278.     }
  279.       if (strcmp(Buffer+4, OldBuffer+4)!=0)
  280.     {
  281.       if (Count>1)
  282.         fprintf(f, ".... %d line%s omitted\n",Count-1, Count==2?"":"s");
  283.       if (Count>0)
  284.         fputs(OldBuffer, f);
  285.       Count=0;
  286.       fputs(Buffer, f);
  287.     }
  288.       else
  289.     Count++;
  290.       strcpy(OldBuffer,Buffer);
  291.     }
  292. }
  293.  
  294. void MemInit(void)
  295. {
  296.   memset(Mem, 0, sizeof(Mem));
  297. }
  298.  
  299.